package com.wuhunyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuhunyu.commonutils.check.Check;
import com.wuhunyu.commonutils.query.CommonQuery;
import com.wuhunyu.commonutils.result.CommonResult;
import com.wuhunyu.entities.CrmBanner;
import com.wuhunyu.mapper.CrmBannerMapper;
import com.wuhunyu.service.CrmBannerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuhunyu.servicebase.exception.Assert;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.security.Key;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 首页banner表 服务实现类
 * </p>
 *
 * @author wuhunyu
 * @since 2021-01-22
 */
@Service("crmBannerService")
public class CrmBannerServiceImpl extends ServiceImpl<CrmBannerMapper, CrmBanner> implements CrmBannerService {

    @Resource
    private CrmBannerMapper crmBannerMapper;

    @Override
    public CommonResult selectAllBanners(CommonQuery commonQuery) {
        // 校验参数
        Assert.isRight(Check.isBlank(commonQuery), "查询参数不能为空");
        // commonQuery
        if (commonQuery.getCurrent() == null || commonQuery.getCurrent() < 0 || commonQuery.getLimit() == null || commonQuery.getLimit() < 0) {
            commonQuery.setCurrent(0);
            commonQuery.setLimit(10);
        }
        // 执行查询操作
        Page<CrmBanner> bannerPage = new Page<>(commonQuery.getCurrent(), commonQuery.getLimit());
        QueryWrapper<CrmBanner> queryWrapper = new QueryWrapper<>();
        // 按sort降序排序
        queryWrapper.orderByDesc("sort");
        bannerPage = crmBannerMapper.selectPage(bannerPage, queryWrapper);
        // 校验参数
        Assert.isRight(Check.isBlank(bannerPage.getRecords()), "查询结果为空");
        // 封装结果集
        HashMap<String, Object> map = new HashMap<>(16);
        map.put("total", bannerPage.getTotal());
        map.put("items", bannerPage.getRecords());
        return CommonResult.success("轮播图信息查询成功", map);
    }

    @Override
    @Cacheable(key = "'banner'", value = "banners")
    public CommonResult selectAllBanners() {
        // 查询全部轮播图，用户界面只显示两张图片
        QueryWrapper<CrmBanner> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");
        queryWrapper.last("limit 2");
        List<CrmBanner> banners = crmBannerMapper.selectList(queryWrapper);
        Assert.isRight(Check.isBlank(banners.size() == 2), "查询结果有误");
        // 封装结果集
        HashMap<String, Object> map = new HashMap<>(16);
        map.put("banners", banners);
        return CommonResult.success("轮播图信息查询成功", map);
    }

    @Override
    @CacheEvict(key = "'banner'", value = "banners")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult addBanner(CrmBanner crmBanner) {
        // 校验参数
        Assert.isRight(Check.isBlank(crmBanner), "添加轮播图参数为空");
        // title
        Assert.isRight(Check.isBlank(crmBanner.getTitle()), "轮播图标题不能为空");
        // 轮播图url
        Assert.isRight(Check.isBlank(crmBanner.getImageUrl()), "轮播图url不能为空");
        // 轮播图链接地址
        Assert.isRight(Check.isBlank(crmBanner.getLinkUrl()), "轮播图链接地址不能为空");
        // 排序
        Assert.isRight(Check.isBlank(crmBanner.getSort()), "轮播图排序错误");
        // title不能重复
        QueryWrapper<CrmBanner> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("title", crmBanner.getTitle());
        Assert.isRight(crmBannerMapper.selectCount(queryWrapper1) > 0, "轮播图标题已存在");
        // 排序不能重复
        QueryWrapper<CrmBanner> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("sort", crmBanner.getSort());
        Assert.isRight(crmBannerMapper.selectCount(queryWrapper2) > 0, "轮播图排序已存在");
        // 执行插入
        Assert.isRight(crmBannerMapper.insert(crmBanner) != 1, "轮播图新增失败");
        return CommonResult.success("轮播图新增成功");
    }

    @Override
    @CacheEvict(key = "'banner'", value = "banners")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult updateBanner(CrmBanner crmBanner) {
        // 校验参数
        Assert.isRight(Check.isBlank(crmBanner), "修改轮播图参数为空");
        // id
        Assert.isRight(Check.isBlank(crmBanner.getId()), "轮播图id不能为空");
        // title
        Assert.isRight(Check.isBlank(crmBanner.getTitle()), "轮播图标题不能为空");
        // 轮播图url
        Assert.isRight(Check.isBlank(crmBanner.getImageUrl()), "轮播图url不能为空");
        // 轮播图链接地址
        Assert.isRight(Check.isBlank(crmBanner.getLinkUrl()), "轮播图链接地址不能为空");
        // 排序
        Assert.isRight(Check.isBlank(crmBanner.getSort()), "轮播图排序错误");
        // title不能重复
        QueryWrapper<CrmBanner> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.ne("id", crmBanner.getId());
        queryWrapper1.eq("title", crmBanner.getTitle());
        Assert.isRight(crmBannerMapper.selectCount(queryWrapper1) > 0, "轮播图标题已存在");
        // 排序不能重复
        QueryWrapper<CrmBanner> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.ne("id", crmBanner.getId());
        queryWrapper2.eq("sort", crmBanner.getSort());
        Assert.isRight(crmBannerMapper.selectCount(queryWrapper2) > 0, "轮播图排序已存在");
        // 执行修改
        Assert.isRight(crmBannerMapper.updateById(crmBanner) < 0, "轮播图修改失败");
        return CommonResult.success("轮播图修改成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult deleteBanner(String id) {
        // 校验参数
        Assert.isRight(Check.isBlank(id), "轮播图id不能为空");
        // 查询此轮播图是否存在
        Assert.isRight(Check.isBlank(crmBannerMapper.selectById(id)), "所要删除的轮播图不存在");
        // 执行删除
        Assert.isRight(crmBannerMapper.deleteById(id) != 1, "轮播图删除失败");
        return CommonResult.success("轮播图删除成功");
    }

}
